package template;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;

import net.java.ao.DatabaseProvider;
import net.java.ao.EntityManager;

import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.PropertyModel;

public class ShareCalendar extends Restricted {
	
	//local variables
	private String selectedCalendar, shareUsername;
	private HashMap<String, Integer> calendarMap, userMap;
	
	//private class for the form that allows override of onSubmit method
	private class ShareCalendarForm extends Form {
		ShareCalendar _shareCalendar;
		ShareCalendarForm(String id, ShareCalendar shareCalendar) {
			super(id);
			_shareCalendar = shareCalendar;
		}
		public void onSubmit() {
			try {
				_shareCalendar.share();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	//constructor
	public ShareCalendar() {
		//constructs instance of restricted page
		super();
		
		//generates hashmap with Calendar Names and their associated ID's
		calendarMap = new HashMap<String, Integer>();
		
		//constructs form to add to page
		ShareCalendarForm form = new ShareCalendarForm("form", this);
		
		//defines user in current session
		Person user = (Person)_session.getUser();
		
		//instance of entitymanager
		EntityManager manager = new EntityManager(DatabaseProvider.getInstance(
				"jdbc:hsqldb:mem:basic-ao", null, null));
		
		//propertymodels for the selected calendar in dropdown and the input username
		PropertyModel calendarModel = new PropertyModel(this, "selectedCalendar");
		PropertyModel usernameModel = new PropertyModel(this, "shareUsername");
		
		//populates the hashmap with the calendarnames as keys and their IDs as values
		try {
			Calendar[] usersCalendars = manager.find(Calendar.class, "creatorID = ?", user.getID());
			LinkedList<String> calendarNames = new LinkedList<String>();
			for(int i=2;i<usersCalendars.length;i++) {
				calendarNames.add(i-2, usersCalendars[i].getName());
				calendarMap.put(usersCalendars[i].getName(), usersCalendars[i].getID());
			}
			
			//form adds dropdown and textfield
			form.add(new DropDownChoice("selectedCalendar", calendarModel, calendarNames));
			form.add(new TextField("shareUsername", usernameModel));
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//layout adds the form
		_layout.add(form);
	}
	public void share() throws SQLException {
		
		//instance of entity manager
		EntityManager manager = new EntityManager(DatabaseProvider.getInstance(
				"jdbc:hsqldb:mem:basic-ao", null, null));
		
		//find all registered users
		Person[] users = manager.find(Person.class);
		
		//instantiate hashmap of users associated with their IDs as values
		userMap = new HashMap<String, Integer>();
		for(int j=0;j<users.length;j++) {
			userMap.put(users[j].getUsername(), users[j].getID());
		}
		
		//if the username doesnt exist, redirect user back to this page
		if(!userMap.containsKey(shareUsername)) {
			this.setResponsePage(this);
			return;
		}
		
		//retrieve selected calendar from hashmap
		calendarMap.get(selectedCalendar);
		
		//use the retrieved ID from hashmap to locate quickly in database
		Calendar calendar = manager.get(Calendar.class, calendarMap.get(selectedCalendar));
		
		//get all events on the calendar
		Event[] calendarEvents = calendar.getEvents();
		
		//get the ID of the user-to-share-with from hashmap
		Person shareUser = manager.get(Person.class, userMap.get(shareUsername));
		
		//get the calendars belonging to the user-to-share-with
		Calendar[] shareUserCalendars = manager.find(Calendar.class, "creatorID = ?", shareUser.getID());
		
		//viewable calendar is the first, events in the calendar being shared are placed here
		Calendar viewableCalendar = shareUserCalendars[0];
		
		//create relations in database for every event on the calendar being shared
		//to the user's viewable events calendar
		for(int i=0;i<calendarEvents.length;i++) {
			EventsToCalendars relation = manager.create(EventsToCalendars.class);
			relation.setCalendar(viewableCalendar);
			relation.setEvent(calendarEvents[i]);
			relation.save();
		}
		//set the response page to View Calendars page
		this.setResponsePage(ViewCalendars.class);
	}
}
